'''
    Weibo Follower Tags method
'''

from MySqlDBHandle import MySqlDBHandle
from WeiboClient import WeiboClient

class Tag(WeiboClient):
    mInsertSQLStatement = "INSERT INTO LU_Tags (Tag_name, LAST_UPDATE_TIMESTAMP) VALUES (%s) ON DUPLICATE KEY UPDATE LAST_UPDATE_TIMESTAMP = current_timestamp;"
    mSelectSQLStatement = "SELECT (*) FROM LU_Tags WHERE Tag_name = %s;"
    
    def _isTagInDB(self, iTagName):
        lSQLStatement = self.mSelectSQLStatement % iTagName
        result = MySqlDBHandle.execSqlStatement(lSQLStatement)
        if result is None:
            return False
        else:
            return True
        
    def _InsertTagToDB(self, iTagName):
        if self._isTagInDB(iTagName):
            return
        lInsertSQLStatement = self.mInsertSQLStatement % iTagName
        try:
            MySqlDBHandle.execSqlStatement(lInsertSQLStatement)
        except Exception, e:
            print "Error in Tag.py InsertTagToDB function with error = " + str(e)
        
    def process(self, iTagName):
        self._InsertTagToDB(iTagName)
        
    def getTagId(self, iTagName):
        lSQLStatement = self.mSelectSQLStatement % iTagName
        result = MySqlDBHandle.execSqlStatement(lSQLStatement)
        if result is not None:
            return result[0][0]
        else:
            self._InsertTagToDB(iTagName)
            try:
                result = MySqlDBHandle.execSqlStatement(lSQLStatement)
                return result[0][0]
            except Exception, e:
                print "Error When get TagId with TagName = " + iTagName
                print "Error message = " + str(e)
                return None
    